﻿using System;

using System.Collections.Generic;
using System.Text;
using Fluid.Controls;
using System.Drawing;
using PasswordSafe.Classes;
using PasswordSafe.Templates;
using PasswordSafe.Xml;
using Fluid.Classes;

namespace PasswordSafe
{
    public class CategoriesListBox:ListBoxBase
    {
        protected override void InitControl()
        {
            base.InitControl();
            //ShowHeader = false;
            ItemHeight = 32;

            template = new CategoryTemplate();
            ItemTemplate = template;

            categories = ListBuilder.Instance.ListBoxCategories;
            categories.ListChanged += new System.ComponentModel.ListChangedEventHandler(categories_ListChanged);
            this.EnableInsertDeleteAnimation = true;

            DataSource = categories;
        }

        void categories_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
        {
       //     DataSource = categories;
        }

        private CategoryTemplate template;

        private NotifyList categories;

        public NotifyList Categories { get { return categories; } }

        protected override void OnSelectedIndexChanged(int oldValue, int newValue)
        {
            template.DeleteIndex = -1;
            base.OnSelectedIndexChanged(oldValue, newValue);
        }

        public CategoryTemplate CategoryTemplate { get { return ItemTemplate as CategoryTemplate; } }


        protected override void OnItemClick(int index)
        {
            base.OnItemClick(index);
            NavigateForward();
        }

        public override bool NavigateForward()
        {
            if (!editMode)
            {
                PasswordCategory category = categories[SelectedItemIndex] as PasswordCategory;

                SelectedCategory = category;
                if (category != null)
                {
                    if (CategorySelected != null) CategorySelected(this, EventArgs.Empty);
                }
            }
            return true;
        }

        public PasswordCategory SelectedCategory { get; private set; }

        public event EventHandler CategorySelected;

        private bool editMode;
        public bool EditMode
        {
            get { return editMode; }
            set
            {
                if (editMode != value)
                {
                    editMode = value;
                    OnEditModeChanged();
                }
            }
        }


        private void OnEditModeChanged()
        {
            bool editMode = EditMode;
            Pages.editCategoryBtn.Text = editMode ? "Done" : "Edit";
            template.EditMode = true;
            Animation.LogAnimation(editMode ? 0 : 100, editMode ? 100 : 0, 150, anim_Scene);
            template.EditMode = editMode;
        }

        void anim_Scene(object sender, AnimationEventArgs e)
        {
            template.ButtonStretch = e.Value;
            Invalidate();
        }


    }
}
